package com.safeluck.scannerqcr.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;
import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.UserBean;
import com.safeluck.scannerqcr.bean.FaceDet;
import com.safeluck.scannerqcr.bean.ResponseBean;
import com.safeluck.scannerqcr.fragment.msg.MessageFragment;
import com.safeluck.scannerqcr.listener.OnTabSelectedListener;
import com.safeluck.scannerqcr.loader.LoaderStyle;
import com.safeluck.scannerqcr.net.RestClient;
import com.safeluck.scannerqcr.net.callback.IError;
import com.safeluck.scannerqcr.net.callback.IFailure;
import com.safeluck.scannerqcr.net.callback.IRequest;
import com.safeluck.scannerqcr.net.callback.ISuccess;
import com.safeluck.scannerqcr.util.ByteUtil;
import com.safeluck.scannerqcr.util.FileUtil;
import com.safeluck.scannerqcr.util.HttpConnectUtil;
import com.safeluck.scannerqcr.view.BottomBar;
import com.safeluck.scannerqcr.view.BottomBarTab;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportFragment;
import retrofit2.Call;

/**
 * ScannerQCR
 *
 * @author lzw
 * @date 2017/11/28
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class MainFragment extends SupportFragment {

    private static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    public static final int FOURTH = 3;
    private static final String TAG = "MainFragment";

    private BottomBar bottonBar;
    private SupportFragment[] mFragments = new SupportFragment[4];


    @Override
    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
        Log.e(TAG,"fjska");
        super.onFragmentResult(requestCode, resultCode, data);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.wechat_fragment_main,container,false);
        initView(view);
        Logger.addLogAdapter(new AndroidLogAdapter());
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        SupportFragment firstFragment = findChildFragment(WechatFirstTabFragment.class);
        if (firstFragment == null){
            mFragments[FIRST] = WechatFirstTabFragment.newInstance();
            mFragments[SECOND] = WechatSecondTabFragment.newInstance();
            mFragments[THIRD] = WechatThirdTabFragment.newInstance();
            mFragments[FOURTH] = WechatFourthTabFragment.newInstance();


            loadMultipleRootFragment(R.id.fl_tab_container,FIRST,
                    mFragments[FIRST],
                    mFragments[SECOND],
                    mFragments[THIRD],
                    mFragments[FOURTH]);
        }else{
            //库里面已经帮做了恢复
            mFragments[FIRST] = firstFragment;
            mFragments[SECOND] = findChildFragment(WechatSecondTabFragment.class);
            mFragments[THIRD] = findChildFragment(WechatThirdTabFragment.class);
            mFragments[THIRD] = findChildFragment(WechatFourthTabFragment.class);
        }
    }

    private void initView(View view) {

        bottonBar = view.findViewById(R.id.bottonbar);
        bottonBar.addItem(new BottomBarTab(_mActivity,R.drawable.ic_message_white_24dp,getString(R.string.msg)));
        bottonBar.addItem(new BottomBarTab(_mActivity,R.drawable.ic_account_circle_white_24dp,getString(R.string.discovery)));
        bottonBar.addItem(new BottomBarTab(_mActivity,R.drawable.ic_discover_white_24dp,getString(R.string.more)));
        bottonBar.addItem(new BottomBarTab(_mActivity,R.drawable.ic_home_white_24dp,getString(R.string.shop)));

        // 模拟未读消息
        bottonBar.getItem(FIRST).setUnreadCount(9);




        bottonBar.setOnTabSelectedListener(new OnTabSelectedListener(){
            @Override
            public void onTabSelected(int position, int prePosition) {
                showHideFragment(mFragments[position], mFragments[prePosition]);




            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                Logger.e("onTabReselected");

            }
        });
    }

    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment mainFragment = new MainFragment();
        mainFragment.setArguments(args);
        return mainFragment;
    }


    public void startBrotherFragment(SupportFragment messageFragment) {
        start(messageFragment);
    }
}
